package com.test.lol;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import java.net.*;

public class ImageActivity extends Activity {
	
	private int pos;		// the id for the photo that is displayed
	
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.imageactivity);	        	        
  
	        Bundle extras = getIntent().getExtras();	
	        
	        if(extras == null){
	        	return;
	        }     
	        
	        pos = extras.getInt("ID");

	        // Displays an image in full scale from url
	        try {			 
				ImageView imageView = (ImageView) findViewById(R.id.imageView);
				TextView captionTextView = (TextView) findViewById(R.id.caption);
								
				URL aURL = new URL(ImageAdapter.io[pos].getUrl());
				URLConnection conn = aURL.openConnection();
				
				conn.connect();
				 
				InputStream is = conn.getInputStream();			
				BufferedInputStream bis = new BufferedInputStream(is);				
				Bitmap bm = BitmapFactory.decodeStream(bis);
				
				bis.close();
				is.close();
				
				imageView.setImageBitmap(bm);
				captionTextView.setText(ImageAdapter.io[pos].getCaption());
								
			} catch (Exception e) {
				Log.e(this.getClass().getName(), "ImageActivity exception: " + e.getMessage());
			}        
	 }
	    @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
			menu.add("Comment (" + ImageAdapter.io[pos].getNumberOfComments()+")");
			return super.onCreateOptionsMenu(menu);
	    }
		/*
		 * Starts a new activity CommentActivity when a user press "read comments"
		 */
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	            case 0:	   	            	
					Intent i = new Intent(ImageActivity.this, CommentActivity.class);
					i.putExtra("ID", ImageAdapter.io[pos].getID());
					startActivity(i);
	            	break;
	        }
	        return true;
	    }
}
